package com.game.modular.game.battle.process.skillEffectHandle.base;

import com.game.comment.util.RedisUtils;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.domain.SkillEffectLayer;
import com.game.modular.game.battle.panel.skillEffect.domain.SkillEffectStatus;
import com.game.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.game.comment.util.BeanUtil.updateField;


/**
 * 保存
 * 技能效果|面板数据
 * * 新增(更新数值|保存状态)
 * * 刷新(衰弱|叠层)
 * * 过期(回滚数值|删除技能效果)
 */
@Slf4j
@Component
public class EffectHandle6 {
    @Resource
    private PlayerHeroAttributeServiceImpl playerHeroAttributeService;
    @Resource
    private PvpSkillEffectServiceImpl pvpSkillEffectService;
    @Resource
    private RedisUtils redisUtils;

    /**
     * 刷新属性面板
     * 回滚的情况->(删除状态|恢复历史变动)
     * 非回滚情况->(刷新状态|更新变动)
     */
    @SuppressWarnings("unchecked")
    public void tryLogAndRefresh(PvpSkillEffect skillEffect) {
        SkillEffectStatus status = skillEffect.getStatus();
        Integer statusStatus = status.getStatus();
        if (statusStatus == 0) {
            return;
        }
        Integer isSave = status.getIsSave();
        if (isSave == 1){
            pvpSkillEffectService.save(skillEffect);
        }

        Integer isTrigger = status.getIsTrigger();
        if (isTrigger == 1) {
            if (statusStatus == 4) {
                log.info(
                        "技能效果({})过期(过期回合数{}),{}--->{}",
                        skillEffect.getCoreSkillEffectTypeName(),
                        status.getStatus4Value(),
                        skillEffect.getSourcePlayerHeroId(),
                        skillEffect.getTargetPlayerHeroId()
                );
                refreshPanel(skillEffect);
                pvpSkillEffectService.remove(skillEffect);
            } else {
                refreshPanel(skillEffect);
                pvpSkillEffectService.toUpdate(skillEffect);
            }
        }
    }

    private void refreshPanel(PvpSkillEffect skillEffect) {
        Long battleId = skillEffect.getBattleId();
        Long targetPlayerHeroId = skillEffect.getTargetPlayerHeroId();
        SkillEffectLayer skillEffectLayer = skillEffect.getSkillEffectLayer();
        String name = skillEffectLayer.getName();
        Double thisChangeValue = skillEffectLayer.getThisChangeValue();
        PlayerHeroAttribute attribute = playerHeroAttributeService.selectCache(battleId, targetPlayerHeroId);
        boolean b = updateField(attribute, name, thisChangeValue);
        if (b) {
            playerHeroAttributeService.saveCache(attribute);
        }
    }
}
